import { Entity, Column, ManyToOne, JoinColumn, OneToMany } from "typeorm";

import { StockNodeLink, BIGINT_PG, TonOrderGroup, StockNodeGateway, StockNodeLinkGateway, INTEGER_PG, StockNode, VARCHAR255_PG, CommonStock } from "qqlx-core";

import { TransformerBigInteger, TransformerInteger } from "@lib/orm.transfor.number";
import { TransformerVarchar255 } from "@lib/orm.transfor.string";
import { TransformerBoolean } from "@lib/orm.transfor.boolean";

import { SkuTplSchema } from "@/alpha/ext.com.tpl";
import { OwnerIdSchema } from "@/alpha/ext.id.base";

@Entity({ name: StockNodeGateway.relations })
export class StockNodeSchema extends SkuTplSchema implements StockNode {
  @Column({ transformer: new TransformerVarchar255() })
  ivid: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerVarchar255() })
  labelStr: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerInteger() })
  layer: INTEGER_PG = 0;

  @Column({ transformer: new TransformerBigInteger() })
  timeStart!: BIGINT_PG;

  @Column({ transformer: new TransformerBigInteger() })
  timeEnd!: BIGINT_PG;

  @Column({ transformer: new TransformerBoolean() })
  isRootNode: boolean = false;

  // ================================
  // ================================
  // ================================

  @Column({
    transformer: new TransformerInteger(1, { nullable: true }),
    nullable: true,
    name: "refRootNodeId", // 明确指定列名
  })
  refRootNodeId!: StockNode["id"];

  @ManyToOne("StockNodeSchema", (e: StockNode) => e.justRootNode, {
    nullable: true, // 关系可以为空
    onDelete: "SET NULL", // 对应 ON DELETE SET NULL
    onUpdate: "CASCADE", // 对应 ON UPDATE CASCADE
  })
  @JoinColumn({ name: "refRootNodeId", referencedColumnName: "id" })
  justRootNode!: StockNode;

  @Column({
    transformer: new TransformerInteger(1, { nullable: true }),
    nullable: true,
    name: "refParentNodeId", // 明确指定列名
  })
  refParentNodeId!: StockNode["id"];

  @ManyToOne("StockNodeSchema", (e: StockNode) => e.justParentNode, {
    nullable: true, // 关系可以为空
    onDelete: "SET NULL", // 对应 ON DELETE SET NULL
    onUpdate: "CASCADE", // 对应 ON UPDATE CASCADE
  })
  @JoinColumn({ name: "refParentNodeId", referencedColumnName: "id" })
  justParentNode!: StockNode;

  // ================================
  // ================================
  // ================================

  @OneToMany("StockNodeLinkSchema", (e: StockNodeLink) => e.justStockNode)
  joinStockNodeLinkList!: StockNodeLink[];

  @OneToMany("CommonStockSchema", (e: CommonStock) => e.justStockNode)
  joinCommonStockList!: CommonStock[];
}

@Entity({ name: StockNodeLinkGateway.relations })
export class StockNodeLinkSchema extends OwnerIdSchema implements StockNodeLink {
  @Column({ transformer: new TransformerInteger() })
  refTonOrderGroupId!: TonOrderGroup["id"];

  @ManyToOne("TonOrderGroupSchema", (e: TonOrderGroup) => e.joinStockNodeLinkList)
  @JoinColumn({ name: "refTonOrderGroupId", referencedColumnName: "id" })
  justTonOrderGroup!: TonOrderGroup;

  @Column({ transformer: new TransformerInteger() })
  refStockNodeId!: StockNode["id"];

  @ManyToOne("StockNodeSchema", (e: StockNode) => e.joinStockNodeLinkList)
  @JoinColumn({ name: "refStockNodeId", referencedColumnName: "id" })
  justStockNode!: StockNode;
}
